home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / SwingSet / ExampleFileView.java < prev    next >
Encoding:
Java Source  |  1998-12-01  |  5.2 KB  |  185 lines

  1. /*
  2.  * @(#)ExampleFileView.java    1.6 98/08/26
  3.  *
  4.  * Copyright 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. import javax.swing.*;
  16. import javax.swing.filechooser.*;
  17.  
  18. import java.io.File;
  19. import java.util.Hashtable;
  20.  
  21. /**
  22.  * A convenience implementation of the FileView interface that
  23.  * manages name, icon, traversable, and file type information.
  24.  *
  25.  * This this implemention will work well with file systems that use
  26.  * "dot" extensions to indicate file type. For example: "picture.gif"
  27.  * as a gif image.
  28.  *
  29.  * If the java.io.File ever contains some of this information, such as
  30.  * file type, icon, and hidden file inforation, this implementation may
  31.  * become obsolete. At minimum, it should be rewritten at that time to
  32.  * use any new type information provided by java.io.File
  33.  *
  34.  * Example:
  35.  *    JFileChooser chooser = new JFileChooser();
  36.  *    fileView = new ExampleFileView();
  37.  *    fileView.putIcon("jpg", new ImageIcon("images/jpgIcon.jpg"));
  38.  *    fileView.putIcon("gif", new ImageIcon("images/gifIcon.gif"));
  39.  *    chooser.setFileView(fileView);
  40.  *
  41.  * @version 1.6 08/26/98
  42.  * @author Jeff Dinkins
  43.  */
  44. public class ExampleFileView extends FileView {
  45.     private Hashtable icons = new Hashtable(5);
  46.     private Hashtable fileNames = new Hashtable(5);
  47.     private Hashtable fileDescriptions = new Hashtable(5);
  48.     private Hashtable typeDescriptions = new Hashtable(5);
  49.  
  50.     /**
  51.      * The name of the file.
  52.      * @see #getName
  53.      */
  54.     public void setName(File f, String fileName) {
  55.     fileNames.put(fileName, f);
  56.     }
  57.  
  58.     /**
  59.      * The name of the file.
  60.      * @see #setName
  61.      * @see FileView#getName
  62.      */
  63.     public String getName(File f) {
  64.     return (String) fileNames.get(f);
  65.     }
  66.  
  67.     /**
  68.      * Adds a human readable description of the file.
  69.      */
  70.     public void putDescription(File f, String fileDescription) {
  71.     fileDescriptions.put(fileDescription, f);
  72.     }
  73.  
  74.     /**
  75.      * A human readable description of the file.
  76.      *
  77.      * @see FileView#getDescription
  78.      */
  79.     public String getDescription(File f) {
  80.     return (String) fileDescriptions.get(f);
  81.     };
  82.  
  83.     /**
  84.      * Adds a human readable type description for files. Based on "dot"
  85.      * extension strings, e.g: ".gif". Case is ignored.
  86.      */
  87.     public void putTypeDescription(String extension, String typeDescription) {
  88.     typeDescriptions.put(typeDescription, extension);
  89.     }
  90.  
  91.     /**
  92.      * Adds a human readable type description for files of the type of
  93.      * the passed in file. Based on "dot" extension strings, e.g: ".gif".
  94.      * Case is ignored.
  95.      */
  96.     public void putTypeDescription(File f, String typeDescription) {
  97.     putTypeDescription(getExtension(f), typeDescription);
  98.     }
  99.  
  100.     /**
  101.      * A human readable description of the type of the file.
  102.      *
  103.      * @see FileView#getTypeDescription
  104.      */
  105.     public String getTypeDescription(File f) {
  106.     return (String) typeDescriptions.get(getExtension(f));
  107.     }
  108.  
  109.     /**
  110.      * Conveinience method that returnsa the "dot" extension for the
  111.      * given file.
  112.      */
  113.     public String getExtension(File f) {
  114.     String name = f.getName();
  115.     if(name != null) {
  116.         int extensionIndex = name.lastIndexOf('.');
  117.         if(extensionIndex < 0) {
  118.         return null;
  119.         }
  120.         return name.substring(extensionIndex+1).toLowerCase();
  121.     }
  122.     return null;
  123.     }
  124.  
  125.     /**
  126.      * Adds an icon based on the file type "dot" extension
  127.      * string, e.g: ".gif". Case is ignored.
  128.      */
  129.     public void putIcon(String extension, Icon icon) {
  130.     icons.put(extension, icon);
  131.     }
  132.  
  133.     /**
  134.      * Icon that reperesents this file. Default implementation returns
  135.      * null. You might want to override this to return something more
  136.      * interesting.
  137.      *
  138.      * @see FileView#getIcon
  139.      */
  140.     public Icon getIcon(File f) {
  141.     Icon icon = null;
  142.     String extension = getExtension(f);
  143.     if(extension != null) {
  144.         icon = (Icon) icons.get(extension);
  145.     }
  146.     return icon;
  147.     }
  148.  
  149.     /**
  150.      * Whether the file is hidden or not. This implementation returns
  151.      * true if the filename starts with a "."
  152.      *
  153.      * @see FileView#isHidden
  154.      */
  155.     public Boolean isHidden(File f) {
  156.     String name = f.getName();
  157.     if(name != null && !name.equals("") && name.charAt(0) == '.') {
  158.         return Boolean.TRUE;
  159.     } else {
  160.         return Boolean.FALSE;
  161.     }
  162.     };
  163.  
  164.     /**
  165.      * Whether the directory is traversable or not. Generic implementation
  166.      * returns true for all directories.
  167.      *
  168.      * You might want to subtype ExampleFileView to do somethimg more interesting,
  169.      * such as recognize compound documents directories; in such a case you might
  170.      * return a special icon for the diretory that makes it look like a regular
  171.      * document, and return false for isTraversable to not allow users to
  172.      * descend into the directory.
  173.      *
  174.      * @see FileView#isTraversable
  175.      */
  176.     public Boolean isTraversable(File f) {
  177.     if(f.isDirectory()) {
  178.         return Boolean.TRUE;
  179.     } else {
  180.         return Boolean.FALSE;
  181.     }
  182.     };
  183.  
  184. }
  185.